using NewsPortal.Domain;
using NewsPortal.Domain.Layout;
using NewsPortal.Domain.PostEntities;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;

namespace NewsPortal.DataAccess.Migrations
{
    internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(ApplicationDbContext context)
        {
            //AddInitialGrid(context);
            //AddDefaultPages(context);
            //AddDefaultCategories(context);
            //AddDefaultPosts(context);
        }

        private void AddDefaultCategories(ApplicationDbContext context)
        {
            PostCategory pc = new PostCategory()
            {
                Title = "Category #1",
                Description = "Desc for cat #1",
                Date = DateTime.Now
            };
            context.PostCategories.Add(pc);
            context.SaveChanges();
        }

        private void AddDefaultPosts(ApplicationDbContext context)
        {
            var pc = context.PostCategories.First();
            Post p = new Post()
            {
                Title = "Title #1",
                Description = "Desc",
                Body = "Body for post #1",
                Date = DateTime.Now,
                Categories = new List<PostCategory>()
            };
            p.Categories.Add(pc);
            context.SaveChanges();
        }

        private void AddDefaultPages(ApplicationDbContext context)
        {
            context.Pages.Add(new Page()
                {
                    Title = "Blog Page..",
                    Description = "this page is used to give user a list of blog posts."
                });
            context.SaveChanges();
        }

        private void AddInitialGrid(ApplicationDbContext context)
        {
            Row row = new Row();

            row.Columns = new List<Column>();
            row.Columns.Add(new Column()
            {
                CssClass = "col-md-4 column"
            });
            row.Columns.Add(new Column()
            {
                CssClass = "col-md-4 column"
            });
            Grid grid = new Grid();
            grid.Rows = new List<Row>();
            grid.Rows.Add(row);

            Page page = new Page()
            {
                Grids = new List<Grid>() { grid }
            };

            context.Pages.Add(page);
            context.SaveChanges();
        }
    }
}
